package org.ishequ.upload;

import java.io.File;

import org.ishequ.service.DownloadTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class Android_UploadActivity extends Activity {
	
	private EditText pathEdit;
	private Button downloadBtn;
	private Button dstopBtn;
	private ProgressBar progressBar;
	private TextView percentText;
	private DownloadTask task;
	
	//hanlder的作用是用于往创建Hander对象所在的线程所绑定的消息队列发送消息
    private Handler handler = new UIHander();
    
    private final class UIHander extends Handler{
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				int size = msg.getData().getInt("size");
				progressBar.setProgress(size);
				float num = (float)progressBar.getProgress() / (float)progressBar.getMax();
				int result = (int)(num * 100);
				percentText.setText(result+ "%");
				if(progressBar.getProgress() == progressBar.getMax()){
					Toast.makeText(getApplicationContext(), R.string.dsuccess, 1).show();
				}
				break;
			case -1:
				Toast.makeText(getApplicationContext(), R.string.derror, 1).show();
				break;
			}
		}
    }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        pathEdit = (EditText) findViewById(R.id.path);
        downloadBtn = (Button) findViewById(R.id.download);
        dstopBtn = (Button) findViewById(R.id.dstop);
        progressBar = (ProgressBar) findViewById(R.id.rectangleProgressBar);
        percentText = (TextView) findViewById(R.id.percent);
        
        downloadBtn.setOnClickListener(new ButtonOnClickListener());
        dstopBtn.setOnClickListener(new ButtonOnClickListener());
    }
    
    private final class ButtonOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.download:
				String path = pathEdit.getText().toString().trim();
				if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
					File saveDir = Environment.getExternalStorageDirectory();
					task = new DownloadTask(path,saveDir,handler,getApplicationContext(),progressBar);
					new Thread(task).start();
				}else{
					Toast.makeText(getApplicationContext(), R.string.sdcarderror, 1).show();
				}
				downloadBtn.setEnabled(false);
				dstopBtn.setEnabled(true);
				progressBar.setVisibility(View.VISIBLE);
				percentText.setVisibility(View.VISIBLE);
				break;

			case R.id.dstop:
				exit();
				downloadBtn.setEnabled(true);
				dstopBtn.setEnabled(false);
				progressBar.setVisibility(View.GONE);
				percentText.setVisibility(View.GONE);
				break;
			}
			
		}
		/**
		 * 退出下载
		 */
		public void exit(){
			if(task!=null) task.exit();
		}
		
    }
    
    
}